package com.example.newheze;
//欢迎页

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.newheze.FieldMessage.AnalogData;
import com.example.newheze.Util.HttpUtils;
import com.example.newheze.vo.Info;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

public class WelcomeActivity extends AppCompatActivity implements View.OnClickListener {
    private int recLen = 3;//跳过倒计时提示3秒
    private TextView tv;
    Timer timer = new Timer();
    private Handler handler;
    private Runnable runnable;

    Intent dataintent;
    Info data;
    private String[] date_soil_humi1= new String[7];
    private float[] info_soil_humi1=new float[7];
    private String[] date_soil_temp1= new String[7];
    private float[] info_soil_temp1=new float[7];
    private String[] date_soil_elect1= new String[7];
    private float[] info_soil_elect1=new float[7];
    private String[] date_soil_humi2= new String[7];
    private float[] info_soil_humi2=new float[7];
    private String[] date_soil_temp2= new String[7];
    private float[] info_soil_temp2=new float[7];
    private String[] date_soil_elect2= new String[7];
    private float[] info_soil_elect2=new float[7];
    private String[] date_sunlight= new String[7];
    private float[] info_sunlight=new float[7];
    private String[] date_co2= new String[7];
    private float[] info_co2=new float[7];
    private String[] date_air_humi= new String[7];
    private float[] info_air_humi=new float[7];
    private String[] date_air_temp= new String[7];
    private float[] info_air_temp=new float[7];



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //定义全屏参数
        int flag= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //设置当前窗体为全屏显示
        getWindow().setFlags(flag, flag);
        setContentView(R.layout.activity_welcome);
        initView();
        timer.schedule(task, 1000, 1000);//等待时间一秒，停顿时间一秒

        new MyAsyncTask().execute("http://202.194.131.174:8080/newdongping/Wenshi_dongping1_servlet?DTU=009&num=7");

        //正常情况下不点击跳过
        handler = new Handler();
        handler.postDelayed(runnable = new Runnable() {
            @Override
            public void run() {
                //从闪屏界面跳转到登录界面
                //Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                startActivity(dataintent);
                //startActivity(intent);
                finish();
            }
        }, 3000);//延迟5S后发送handler信息

    }

    private void initView() {
        tv = findViewById(R.id.tv);//跳过
        tv.setOnClickListener(this);//跳过监听
    }

    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() { // UI thread
                @Override
                public void run() {
                    recLen--;
                    tv.setText("跳过 " + recLen);
                    if (recLen < 0) {
                        timer.cancel();
                        tv.setVisibility(View.GONE);
                        //倒计时到0隐藏字体
                    }
                }
            });
        }
    };

    //点击跳过

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv:
                //从闪屏界面跳转到首界面
                //Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                startActivity(dataintent);
                //startActivity(intent);
                finish();
                if (runnable != null) {
                    handler.removeCallbacks(runnable);
                }
                break;
            default:
                break;
        }
    }


    public class MyAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            try {
                JSONArray jsonArray = new JSONArray(s);
                for(int i=0;i<jsonArray.length();i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    data = new Info(jsonObject.getString("datetime"), jsonObject.getString("soil_humi1"), jsonObject.getString("soil_temp1"), jsonObject.getString("soil_elect1"), jsonObject.getString("soil_humi2"), jsonObject.getString("soil_temp2"), jsonObject.getString("soil_elect2"), jsonObject.getString("sunlight"), jsonObject.getString("co2"), jsonObject.getString("air_humi"), jsonObject.getString("air_temp"), jsonObject.getString("DTU"));
                    date_soil_humi1[i]=data.getDatetime();
                    info_soil_humi1[i]= Float.parseFloat(data.getSoil_humi1());
                    date_soil_temp1[i]=data.getDatetime();
                    info_soil_temp1[i]= Float.parseFloat(data.getSoil_temp1());
                    date_soil_elect1[i]=data.getDatetime();
                    info_soil_elect1[i]= Float.parseFloat(data.getSoil_elect1());
                    date_soil_humi2[i]=data.getDatetime();
                    info_soil_humi2[i]= Float.parseFloat(data.getSoil_humi2());
                    date_soil_temp2[i]=data.getDatetime();
                    info_soil_temp2[i]= Float.parseFloat(data.getSoil_temp2());
                    date_soil_elect2[i]=data.getDatetime();
                    info_soil_elect2[i]= Float.parseFloat(data.getSoil_elect2());
                    date_sunlight[i]=data.getDatetime();
                    info_sunlight[i]= Float.parseFloat(data.getSunlight());
                    date_co2[i]=data.getDatetime();
                    info_co2[i]= Float.parseFloat(data.getCo2());
                    date_air_humi[i]=data.getDatetime();
                    info_air_humi[i]= Float.parseFloat(data.getAirhumi());
                    date_air_temp[i]=data.getDatetime();
                    info_air_temp[i]= Float.parseFloat(data.getAirtemp());
                }
                AnalogData analogData=new AnalogData();

                analogData.setDate_soil_humi1(date_soil_humi1);
                analogData.setInfo_soil_humi1(info_soil_humi1);
                analogData.setDate_soil_temp1(date_soil_temp1);
                analogData.setInfo_soil_temp1(info_soil_temp1);
                analogData.setDate_soil_elect1(date_soil_elect1);
                analogData.setInfo_soil_elect1(info_soil_elect1);
                analogData.setDate_soil_humi2(date_soil_humi2);
                analogData.setInfo_soil_humi2(info_soil_humi2);
                analogData.setDate_soil_temp2(date_soil_temp2);
                analogData.setInfo_soil_temp2(info_soil_temp2);
                analogData.setDate_soil_elect2(date_soil_elect2);
                analogData.setInfo_soil_elect2(info_soil_elect2);
                analogData.setDate_sunlight(date_sunlight);
                analogData.setInfo_sunlight(info_sunlight);
                analogData.setDate_co2(date_co2);
                analogData.setInfo_co2(info_co2);
                analogData.setDate_air_humi(date_air_humi);
                analogData.setInfo_air_humi(info_air_humi);
                analogData.setDate_air_temp(date_air_temp);
                analogData.setInfo_air_temp(info_air_temp);
                Bundle mBundle = new Bundle();
                mBundle.putParcelable("Data", analogData);//压入数据
                dataintent = new Intent();
                dataintent.setClass(WelcomeActivity.this, MainActivity.class);
                dataintent.putExtras(mBundle);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        protected String doInBackground(String... strings) {
            return HttpUtils.sendPostMethod(strings[0],"utf-8");
        }
    }
}
